package cn.lanqiao.facelogin.faceData;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.SearchFaceRequest;
import com.aliyun.facebody20191230.models.SearchFaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FaceSearch {
    public static boolean m1() throws Exception {
//       拿到URL
        String url = ViapiFileUtilAdvance.ImageTranscoding();
        Boolean faceQuery = LiveFaceDetection.faceQuery(url);
        if (faceQuery) {
            SearchFaceResponse searchFace = searchFace("demoTwo4", url, 5);
            String o1 = com.aliyun.teautil.Common.toJSONString((searchFace));
            System.out.println(o1);
            if ("null".equals(o1)) {
                return false;
            } else {
                List<Map<String, Object>> mapList = new ArrayList<>();
                JSONObject jsonObject = JSONObject.parseObject((String) o1);
                JSONObject body = jsonObject.getJSONObject("body");
                JSONObject data = body.getJSONObject("data");
                JSONArray jsonArray = data.getJSONArray("matchList");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
//            String qualitieScore = jsonObject1.getString("QualitieScore");
                    String location = jsonObject1.getString("location");
                    String faceItems = jsonObject1.getString("faceItems");
                    JSONArray faceItemsArray = JSONObject.parseArray(faceItems);
                    for (int j = 0; j < faceItemsArray.size(); j++) {
                        JSONObject o = faceItemsArray.getJSONObject(j);
                        HashMap<String, Object> hashMap = new HashMap<>();
                        hashMap.put("value", o.get("score"));
                        hashMap.put("faceId",o.get("faceId"));
                        mapList.add(hashMap);
                        System.out.println(mapList);
                    }
                }
                for (int i = 0; i < mapList.size(); i++) {
                    Map<String, Object> Map = mapList.get(i);
                    Object o = Map.get("value".toString());
                    double d = Double.valueOf(String.valueOf(o)).doubleValue();
                    double a = 0.62;
                    if (d > a) {
                        Object faceId = Map.get("faceId".toString());
                        System.out.println(faceId);
                        return true;
                    } else {
                        return false;
                    }
                }
            }
            return false;
        }
        return false;
    }
    /**
     * 搜索人脸
     * @param dbName 数据库名称
     * @param imageUrl 图片URL地址。必须是同Region的OSS地址
     * @param limit 搜索结果数量限制
     * @return Facebody.SearchFaceResponse
     * @throws Exception
     */
    public static SearchFaceResponse searchFace(String dbName, String imageUrl, Integer limit) throws Exception {
        try {
            Client client = createClient("accessKeyId", "accessKeySecret");
            SearchFaceRequest requestBody = new SearchFaceRequest();
            requestBody.dbName = dbName;
            requestBody.imageUrl = imageUrl;
            requestBody.limit = limit;
            return client.searchFace(requestBody);
        } catch (TeaException err) {
            com.aliyun.teaconsole.Client.log("search face error.");
            com.aliyun.teaconsole.Client.log(err.message);
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            com.aliyun.teaconsole.Client.log("search face error.");
            com.aliyun.teaconsole.Client.log(err.message);
        }
        return null;
    }
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId("LTAI5tHNDzFHTvU8ZuETPGY9")
                // 您的AccessKey Secret
                .setAccessKeySecret("FxOXeVcFkA8Q7UuHFnTxGQDnSGqkhb");
        // 访问的域名
        config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new Client(config);
    }
}
